<template>
  <view class="form">
    <input v-model="form.name" placeholder="宠物名字" />
    <input v-model="form.type" placeholder="宠物类型" />
    <input v-model="form.breed" placeholder="品种" />
    <picker :range="['未知','公','母']" @change="e=>form.gender=e.detail.value">
      <view>性别：{{ ['未知','公','母'][form.gender] }}</view>
    </picker>
    <input v-model="form.age" type="number" placeholder="年龄" />
    <input v-model="form.weight" type="digit" placeholder="体重" />
    <textarea v-model="form.remark" placeholder="备注"></textarea>
    <button @click="submit">保存修改</button>
  </view>
</template>

<script>
import request from '@/utils/request.js'

export default {
  data() {
    return { form: {} }
  },
  onLoad(options) {
    this.loadDetail(options.id)
  },
  methods: {
    async loadDetail(id) {
      const user = uni.getStorageSync('user')
      const res = await request.get(`/pet/${id}`, { userId: user.userId })
      if (res.code === 200) this.form = res.data
    },
    async submit() {
      const res = await request.put('/pet/update', this.form)
      if (res.code === 200) {
        uni.showToast({ title: '修改成功', icon: 'success' })
        uni.navigateBack()
      }
    }
  }
}
</script>
